<!--二级集团环保检查计划制定 /checkPlan-->
<template>
  <Layout>
    <Form ref="queryForm" :model="dataToQuery" :label-width="120">
      <Row v-show="!switchFlag" class="query-form">
        <Col span="8">
          <FormItem label="所属二级集团：">
            <Select class='w' placeholder="请选择" v-model='dataToQuery.twoLevelOrgId'>
              <Option v-for="item in twoLevel" :value="item.orgId" :key="item.orgId">{{ item.orgName }}</Option>
            </Select>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label='计划年月：'>
            <DatePicker class="w" type="month"
                        :transfer="true" placeholder="请选择计划年月"
                        v-model='dataToQuery.planDate'
                        @on-change="setPlanDate"></DatePicker>
          </FormItem>
        </Col>
        <Col span="8">
          <Button @click="switchQuery(true)" class="ivu-btn-switch" type="text">展开∨</Button>
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8">
          <FormItem label="所属二级集团：">
            <Select class='w' placeholder="请选择" v-model='dataToQuery.twoLevelOrgId'>
              <Option v-for="item in twoLevel" :value="item.orgId" :key="item.orgId">{{ item.orgName }}</Option>
            </Select>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label='计划年月：' prop='planDate'>
            <DatePicker class="w" type="month" :transfer="true" placeholder="请选择计划年月"
                        v-model='dataToQuery.planDate'></DatePicker>
          </FormItem>
        </Col>
        <Col span="8">
          <FormItem label='计划主题：' prop='planTitle'>
            <Input type='text' v-model='dataToQuery.planTitle'></Input>
          </FormItem>
        </Col>
      </Row>
      <Row v-show="switchFlag" class="query-form">
        <Col span="8" offset="16">
          <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text">收起∧</Button>
          <Button @click="reset" class="ivu-btn-reset">重置</Button>
          <Button class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
        </Col>
      </Row>
      <Row class="query-form">
        <Col span="24">
          <Button size="large" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>
          <!--          <Button type="primary" class="ivu-btn-add" icon="md-cloud-download" @click="exportFormSubmit">导出</Button>-->
          <download-button :api="exportUrl" :query-form="dataToQuery"/>
        </Col>
      </Row>
    </Form>
    <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!" @on-selection-change='getSelectRows'
           :loading='pageStatus.loading' @on-sort-change="sortChange"
           border :columns="columns1" :data="dataTable">
    </Table>
    <Row style="margin-right:10px;">
      <Col span="24">
        <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize" @on-change="pageChange"
              @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray" show-total show-sizer
              show-elevator/>
      </Col>
    </Row>
    <Modal :title="getModalTitle('二级集团'+(this.planType == '2'?'环保':'安全')+'检查计划')" :transfer=false :mask-closable="false"
           v-model="dialogShow"
           v-if="dataToView && dialogShow" :width=800 :styles="{height:'350px'}" class-name='limit-height-dialog'>
      <Row>
        <Col span="24">
          <Form ref="dialogForm" :class="mode === 'view'?'modal-view-detail':'modal-form'" :show-message="false"
                :model="dataToView" :rules="mode === 'view'?{} :ruleCustom"
                :label-position="mode === 'view'?'right':'top'" :label-width="mode === 'view'?160:null">
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="23">
                <FormItem label='所属二级集团：' prop='twoLevelOrgId'>
                  <div v-if="mode === 'view'">{{ dataToView.twoLevelOrgName }}</div>
                  <!--                  <Input v-else class=''placeholder="请填写所属二级集团" type='text' v-model='dataToView.twoLevelOrgName'></Input>-->
                  <Select v-else class='w' placeholder="二级集团" v-model='dataToView.twoLevelOrgId'>
                    <Option v-for="item in twoLevel" :value="item.orgId" :key="item.orgId">{{ item.orgName }}</Option>
                  </Select>
                </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='检查计划主题：' prop='planTitle'>
                  <div v-if="mode === 'view'">{{ dataToView.planTitle }}</div>
                  <Input v-else class='' show-word-limit :maxlength="100" placeholder="请填写检查计划主题" type='text'
                         v-model='dataToView.planTitle'></Input>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='计划年月：' prop='planDate'>
                  <div v-if="mode === 'view'">{{ renderViewMonth(dataToView.planDate) }}</div>
                  <DatePicker v-else :options="options" class="w" type="month" :transfer="true" placeholder="请选择计划年月"
                              v-model='dataToView.planDate'></DatePicker>
                </FormItem>

              </Col>
            </Row>
            <Row type="flex" justify="space-around" class="code-row-bg">
              <Col span="11">
                <FormItem label='填表人：' prop='createUserName'>
                  <div v-if="mode === 'view'">{{ dataToView.createUserName }}</div>
                  <Input v-else type='text' v-model='dataToView.createUserName' readonly></Input>
                </FormItem>
              </Col>
              <Col span="11">
                <FormItem label='填表时间：' prop='createUserName'>
                  <div v-if="mode === 'view'">{{ renderViewDate(dataToView.createDate) }}</div>
                  <DatePicker v-else type="date" placeholder="请选择日期" class="w" v-model='dataToView.createDate'
                              readonly></DatePicker>
                </FormItem>
              </Col>
            </Row>
          </Form>
        </Col>
      </Row>
      <div slot="footer">
        <Button v-if="mode != 'view'" type="primary" class='ivu-btn-save' @click="submit('dialogForm')">确定</Button>
        <Button v-if="mode != 'view'" type="primary" class='ivu-btn-cancel' @click="handleCancel">取消</Button>
        <Button v-if="mode === 'view'" class='ivu-btn-save' @click="dialogShow=false">关闭</Button>
      </div>
    </Modal>
  </Layout>
</template>

<script>
import {Api} from '@/api/index.js'
import {sessionStorage} from '@/common/cacheUtils.js';
import pageCommonFunction from '@/components/pageCommonFunction.js'

var cascaderDom = () => import('@/components/cascader-dom.vue');

export default {
  components: {
    cascaderDom
  },
  data() {
    return {
      ...this.createCommonDataField(),
      options: {
        disabledDate(date) {
          let dateNow = new Date();
          return date && date.valueOf() < dateNow.setMonth(dateNow.getMonth() - 1);
        }
      },
      columns1: [
        {title: '序号', width: 80, fixed: 'left', align: 'center', render: this.indexColumnsRender},
        {key: 'twoLevelOrgName', title: '所属二级集团', align: 'center', minWidth: 120},
        {key: 'planTitle', title: '检查计划主题', align: 'center', minWidth: 120},
        {
          key: 'planDate',
          title: '计划年月',
          sortable: "custom",
          align: 'center',
          minWidth: 120,
          render: this.renderCellMonth
        },
        {key: 'createUserName', title: '填报人', align: 'center', minWidth: 120},
        {
          key: 'createDate',
          title: '填报时间',
          align: 'center',
          sortable: "custom",
          minWidth: 120,
          render: this.renderCellDate
        },
        {
          title: '操作',
          key: 'action',
          fixed: 'right',
          width: 220,
          align: 'center',
          render: (h, params) => {
            if (this.currentUser.loginId === this.Common.admin) {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px',},
                  on: {
                    click: () => {
                      this.toPath(params);
                    }
                  }
                }, '检查计划明细'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  disabled: true,
                  style: {fontSize: '14px'},
                  on: {
                    click: () => {
                      this.editData(params);
                    }
                  }
                }, '修改'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: 'red'},
                  on: {
                    click: () => {
                      this.deleteData(params);
                    }
                  }
                }, '删除')
              ]);
            }
            if (this.comparedDate(params.row.planDate)) {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px',},
                  on: {
                    click: () => {
                      this.toPath(params);
                    }
                  }
                }, '检查计划明细'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '修改'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '删除')
              ]);
            }
            if (this.currentUser.loginId === this.Common.admin || this.currentUser.userId === params.row.createUserId || this.currentUser.userRole.find(obj => obj.roleId == '73b38b37dff64392ba0ee7dd579bf293') != undefined ||
                    this.currentUser.userBeHandedOver.find(obj => obj == params.row.createUserId) != undefined) {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px',},
                  on: {
                    click: () => {
                      this.toPath(params);
                    }
                  }
                }, '检查计划明细'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  disabled: true,
                  style: {fontSize: '14px'},
                  on: {
                    click: () => {
                      this.editData(params);
                    }
                  }
                }, '修改'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: 'red'},
                  on: {
                    click: () => {
                      this.deleteData(params);
                    }
                  }
                }, '删除')
              ]);
            } else {
              return h('div', [
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px',},
                  on: {
                    click: () => {
                      this.toPath(params);
                    }
                  }
                }, '检查计划明细'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '修改'),
                h('Divider', {props: {type: 'vertical'}}, ''),
                h('A', {
                  props: {type: 'primary', size: 'small'},
                  style: {fontSize: '14px', color: '#c5c8ce'}
                }, '删除')
              ]);
            }
          }
        }
      ],
      ruleCustom: {
        twoLevelOrgId: [{required: true, message: '请填写所属二级集团', trigger: 'change',}],
        planTitle: [{required: true, message: '请填写检查计划主题', trigger: 'change',}],
        planDate: [{required: true, message: '请填写计划年月', trigger: 'change', type: 'date'}],
      },
      planType: this.$route.query.planType,
      twoLevel: [],
      tableHeight: 10,
      // api
      queryUrl: Api.queryCheckPlanUrl,
      exportUrl: Api.exportCheckPlanUrl,
      deleteUrl: Api.deleteCheckPlanUrl,
      updateUrl: Api.updateCheckPlanUrl,
      saveUrl: Api.saveCheckPlanUrl,
    }
  },
  mounted() {
    this.$http.get(Api.quqeryTwoOrgInfo).then(res => {
      this.twoLevel = res.data.data
    })
    this.setTableHeight();
    this.query();
  },
  methods: {
    ...pageCommonFunction,
    comparedDate(date) {
      let nowDate = new Date();
      let planDate = new Date(date);
      //当前年份大于 计划年份
      if (nowDate.getFullYear() > planDate.getFullYear()) {
        return true;
      } else if (nowDate.getFullYear() === planDate.getFullYear()) {
        let nowMonth = nowDate.getMonth() + 1;
        let planDateMonth = planDate.getMonth() + 1;
        return nowMonth > planDateMonth;
      } else {
        return false;
      }
    },
    deleteData(params) {
      this.deleteDateAjax(params);
    },
    toPath(params) {
      this.$router.push({path: '/checkPlanItem', query: {id: params.row.id, planType: this.planType}});
    },
    query() {
      this.dataToQuery.planType = this.planType;
      console.log(this.dataToQuery.planDate)
      this.queryAjax();
    },
    submit(name) {
      this.submitAjax(name);
    },
    setPlanDate(date) {
      this.$set(this.dataToQuery, 'planDate', this.renderViewMonth(date))
    },
    getNextM() { //获取下个月第一天
      let nowDate = new Date();
      return nowDate.getFullYear() + "-" + (parseInt(nowDate.getMonth() + 2) < 10 ? "0" + parseInt(nowDate.getMonth() + 2) : parseInt(nowDate.getMonth() + 2)) + "-01";
    },
    createEmptyDataToAdd() {
      return {
        planType: this.planType,
        twoLevelOrgId: sessionStorage('userId').getObject('userId').twoLevelOrgId,
        planDate: this.getNextM()
      }
    }
  },
  watch: {},

}
</script>

<style lang='css' scoped>

</style>
